package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.AllProjectr2Adapter;
import com.wnhz.work.yz.bean.AllProjectBean;
import com.wnhz.work.yz.bean.ProjectChildBean;
import com.wnhz.work.yz.bean.RecordUsedListBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecordUsedListActivity extends BaseActivity implements View.OnClickListener {
    private String recordid;
    private ListView listView;
    private AllProjectr2Adapter adapter;
    private List<AllProjectBean> allBean = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_used_list);
        initTitle();
        initView();
        initData();

    }


    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
    }


    private void initData() {
        recordid = getIntent().getStringExtra("recordid");
        upLoad();
    }

    private List<RecordUsedListBean.InfoEntity> info = new ArrayList<>();

    private void upLoad() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("recordid", recordid);
        XUtil.Post(Url.API_RECORDUSEDLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result);
                Gson gson = new Gson();
                RecordUsedListBean bean = gson.fromJson(result, RecordUsedListBean.class);
                if ("1".equals(bean.getResult())) {
                    info = bean.getInfo();
                   // Log.e("----info",info.toString());
                    handler.sendEmptyMessage(1);
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    List<ProjectChildBean> data = new ArrayList<ProjectChildBean>();

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            allBean.clear();
//            for (int i = 0; i < info.size(); i++) {
//                AllProjectBean ab = new AllProjectBean();
//                ab.setBudget_id(info.get(i).getBudget_id());
//                ab.setBudget_no(info.get(i).getBudget_no());
//                ab.setBudget_status(info.get(i).getBudget_status());
//                ab.setTotal_fee(info.get(i).getTotal_fee());
//                ab.setOrder_desc(info.get(i).getOrder_desc());
//                ab.setCount(info.get(i).getCount());
//                ab.setRecord_name(info.get(i).getRecord_name());
//                ab.setRecord_id(info.get(i).getRecord_id());
//                ab.setBudget_no(info.get(i).getBudget_no());
//                data.clear();
//                List<RecordUsedListBean.InfoEntity.DataEntity> data1 = info.get(i).getData();
//                for (int j = 0; j < data1.size(); j++) {
//                    ProjectChildBean projectChildBean = new ProjectChildBean();
//                    projectChildBean.setGoods_name(data1.get(j).getGoods_name());
//                    projectChildBean.setPackage_cate(data1.get(j).getPackage_cate());
//                    projectChildBean.setGoods_pic(data1.get(j).getGoods_pic());
//                    projectChildBean.setSku_info(data1.get(j).getSku_info());
//                    projectChildBean.setPrice(data1.get(j).getPrice());
//                    projectChildBean.setColor(data1.get(j).getColor());
//                    projectChildBean.setNums(data1.get(j).getNums());
//                    projectChildBean.setRoom_number(data1.get(j).getRoom_number());
//                   Log.e("-----"+j,data1.get(j).getNums()+"==="+data1.get(j).getGoods_name());
//                    data.add(projectChildBean);
//
//                }
//                ab.setData(data);
//                allBean.add(ab);
//            }
            Log.e("---", allBean.size() + "");
            Log.e("----allBean",allBean.toString());
            adapter = new AllProjectr2Adapter(RecordUsedListActivity.this, info, RecordUsedListActivity.this);
            listView.setAdapter(adapter);
        }
    };

    @Override
    protected void onDestroy() {
        if (handler != null)
            handler.removeCallbacks(null);
        super.onDestroy();
    }

    private void initTitle() {
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("引用记录");
    }

    private int index = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            //项目详情
            case R.id.my_listView:
            case R.id.ll_click:
                index = (int) v.getTag();
                Intent intent = new Intent(this, ProjectDetailsActivity.class);
                intent.putExtra("budget_id", allBean.get(index).getBudget_id());
                intent.putExtra("tv_project_code",allBean.get(index).getRecord_name());
                startActivity(intent);
                break;

        }
    }
}
